﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.ComponentModel;
using System.IO;
using Signage01.dao;
using System.Text.RegularExpressions;

namespace Signage01.Dialogs
{
    /// <summary>
    /// Interaction logic for AddTimeline.xaml
    /// </summary>
    public partial class AddTimeline : Window
    {

        // start window closing

        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        // end window closing
        private List<ScreenTemplate> listST;
        private List<Timeline> listTimeline;
        private Campaign campaign;


        public AddTimeline()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            
            // set initial value of duration spinner
            // const arg holo, hr,min,sec
            TimeSpan timeSpan = new TimeSpan();
            durationSpinner.Value = timeSpan;

        }

        
        public AddTimeline(Campaign campaign)
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //this.listST = listST1;
            ScreenTemplateDAO screenTemplateDAO = ScreenTemplateDAO.getSingleTon();
            this.listST = screenTemplateDAO.getAllByScreen(campaign.Screen);
            this.campaign = campaign;
            List<ScreenTemplate> templistST = new List<ScreenTemplate>();
            foreach (ScreenTemplate s in listST)
            {
                //Console.WriteLine(s.ImageFilePath);
                ScreenTemplate st = s.ShallowCopy();
                st.ImageFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + st.ImageFilePath;
                templistST.Add(st);
                //Console.WriteLine(st.ImageFilePath);
            }

            textboxTimelineName.Text = "Timeline " + campaign.LastTimelineIndex;
            listBoxScreenTemplate.ItemsSource = templistST;
            
            // set initial value of duration spinner
            // const arg holo, hr,min,sec
            TimeSpan timeSpan = new TimeSpan();
            durationSpinner.Value = timeSpan;
            
        }

        private void buttonNext_Click_1(object sender, RoutedEventArgs e)
        {
            grid1timeline.Visibility = Visibility.Collapsed;
            gridscreendivision.Visibility = Visibility.Visible;
        }

        private void buttonCancel_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonPrevious_Click_1(object sender, RoutedEventArgs e)
        {
            grid1timeline.Visibility = Visibility.Visible;
            gridscreendivision.Visibility = Visibility.Collapsed;
        }

        private void buttonFinish_Click_1(object sender, RoutedEventArgs e)
        {
            if (listBoxScreenTemplate.SelectedIndex == -1)
            {
                MessageBox.Show("Select a screen template!");
                return;
            }
            storeTimelineObject();
            this.DialogResult = true;
            AddTimeline_Closing(sender, null);
            this.Close();
            
        }

        private void buttonCancel2_Click_1(object sender, RoutedEventArgs e)
        {
            AddTimeline_Closing(sender, null); 
            this.DialogResult = false;            
            this.Close();            
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            var hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
        }

        private void AddTimeline_Closing(object sender, CancelEventArgs e)
        {   
            this.Owner.ShowActivated = true;
            Console.WriteLine("closing is called");
            
        }

        private void storeTimelineObject()
        {
            Timeline timeline = new Timeline();
            timeline.Name = "Timeline " + campaign.LastTimelineIndex;
            TimeSpan timelineDuration = (TimeSpan)durationSpinner.Value;
            timeline.Hr = timelineDuration.Hours;
            timeline.Min = timelineDuration.Minutes;
            timeline.Sec = timelineDuration.Seconds;
            timeline.ScTemplate = (ScreenTemplate)listBoxScreenTemplate.SelectedItem;
            timeline.Sequence = campaign.LastTimelineIndex;
            campaign.LastTimelineIndex++;

            foreach (ScreenDivision screenDivision in timeline.ScTemplate.ListScreenDivision)
            {
                Channel channel = new Channel();
                channel.ZOrder = screenDivision.RectangleDiv.ZIndex;
                timeline.ListChannel.Add(channel);
            }


            campaign.addTimeline(timeline);

            /*
            TimelineDAO timelineDao = TimelineDAO.getTimelineSingleTon();
            int success = timelineDao.saveTimelineObject(timeline);
            Console.WriteLine("Success " + success);
            */
            CampaignDAO campaignDAO = CampaignDAO.getSingleTon();
            campaignDAO.updateCampaign(campaign);

            
        }

        private void textboxTimelineName_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            string inputName = (sender as TextBox).Text;

            if (labelNameValidation != null)
            {
                if (Regex.IsMatch(inputName, @"[a-zA-Z][\w]{4,7}"))
                {
                    labelNameValidation.Content = "";
                    buttonNext.IsEnabled = true;
                }
                else
                {
                    labelNameValidation.Content = "Name must start with alphabet and contain 5-8 alphanumeric characters";
                    buttonNext.IsEnabled = false;
                }
            }


        }// end storetimeline

        
    }
}
